/**
 * Created by jackliu on 15/12/6.
 */

import SJGPage = require('./../kernel/cont/SJGPage');
import ObjManager = require('./../kernel/cont/ObjManager');
import TextObj = require('./../kernel/obj/TextObj');
import GroupObj = require('./../kernel/obj/GroupObj');
import ReplaceGroupObj = require('./../kernel/obj/ReplaceGroupObj');
import SJGReplaceGroup = require('./../kernel/replace/SJGReplaceGroup');
import ObjPositionAlign = require('./../kernel/base/ObjPositionAlign');
import SJGTypeSetBoxUtil2 = require('./SJGTypeSetBoxUtil2');
var async  = require( 'async'); // 同步控制
export = SJGTypeSetItemBase;

class SJGTypeSetItemBase{

    public page:SJGPage = null;

    // 组 排版核心
    // 参数：p页面
    // 参数：group标题组
    // 参数：callback 为async的回调函数，例如：asyncCallback(null, 'one');
    public do(p:SJGPage,group: SJGReplaceGroup ,callback) {
        var objMgr:ObjManager = p.getGlobalData().objManager();
        var THIS = this;
        this.page = p;

        // 文字组对象的宽高，保持不变，根据可替换数组的高级属性计算每个文字的位置
        var groupObj:GroupObj = <GroupObj>objMgr.getObject(group.id);
        if(groupObj){
            var oldIds:Array<number> = groupObj.getChildIds();
            var poss:Array<ObjPositionAlign> = SJGTypeSetBoxUtil2.calcPosition(group.advance, groupObj.pos.w, groupObj.pos.h);
            var len = group.value.length;
            if(len>0){
                //
                for(var i=0;i<len;i++){
                    this.typesetObject(p, group,i, poss[i]);
                }
            }

            // 删除原文档文字组中多余的子对象
            if(len<oldIds.length){
                oldIds.splice(0 ,len);
                groupObj.removeChilds(oldIds);
            }

            // 根据扩展方式以及对齐点调整内部对象的位置
            var replaceGroupObj:ReplaceGroupObj=<ReplaceGroupObj>groupObj;
            replaceGroupObj.refresh();
        }


        // 回调
        //this.after(p, group, callback);
        callback(null, null);
    }

    // 排版后到调整：例如文字组需要增减字号达到填满效果
    // 参数：p页面
    // 参数：group标题组
    // 参数：callback 为async的回调函数，例如：asyncCallback(null, 'one');
    public after(p:SJGPage,group: SJGReplaceGroup ,callback) {
        console.log("------------>SJGTypeSetItemBase after!");
        callback(null, null);
    }

    // 根据坐标和宽高排版对象，需要被重载
    // 参数：p页面
    // 参数：group标题组
    // 参数：i可替换对象的下标
    // 参数：pos对象的宽高
    public typesetObject(p:SJGPage,group: SJGReplaceGroup,i:number, pos:ObjPositionAlign){

    }
}
